package org.opens.javaskill.thread.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UseThreadPoolExecutor {

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new Task()).start();
        }
    }

}

class UseCacheThreadPool {

    /**
     * 说明:
     *      1. CachedThreadPool线程池会要优先执行先分配的任务.
     */
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new Task());
        }
        executorService.shutdown();
    }

}

class UseSingleThreadPool {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            executorService.execute(new Task());
        }
        executorService.shutdown();
    }

}

class FixedThreadPool {

    /**
     * 说明:
     *      1. SingleThreadExecutor线程池中始终只有一个线程, 其运行特征是: `下一个任务开始前, 上一个任务必须结束`.
     */
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new Task());
        }
        executorService.shutdown();
    }

}